package com.muli.muwai.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class TaoBaoApi {
	/* // 正式环境
	 private static String url = "http://gw.api.taobao.com/router/rest";

	 // 沙箱地址
	 private static String urlTest = "http://gw.api.tbsandbox.com/router/rest";

	 // appkey
	 private static String appKey = "31475124";

	 // appSecret
	 private static String appSecret = "31cd3b5dde7e49382331b3b803ed5239";

	 // v 2.0
	 private static String v = "2.0";

	 // 推广位ID
	 private static Long adzone_id = 110965850119L;

	 private static HttpClientUtil httpClientUtil = new HttpClientUtil();

	 @Autowired
	 private ActivityService activityService;


	*//**
	 * 同步淘宝活动信息转链
	 *//*
	public void sycnActivityLink(String materialId){
		// 接口方法名
		String method = "taobao.tbk.activity.info.get";
		String timeStamp = (DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
		// 公共参数加密
		Map<String,String> map = new HashMap<String,String>();
		map.put("method",method);
		map.put("app_key",appKey);
		map.put("timestamp",timeStamp);
		map.put("v",v);
		map.put("sign_method","hmac");
		map.put("format","json");
		// 接口参数加密
		map.put("activity_material_id",materialId);
		map.put("partner_id","top-apitools");
		map.put("adzone_id",String.valueOf(adzone_id));
		String sign="";
		// sign 加密
		try {
			sign = TaoBaoSecretUtil.signTopRequest(map,appSecret,"hmac");
			log.info("加密成功：sign值为{}",sign);
		} catch (IOException e) {
			log.info("加密失败:{}",e.getMessage());
			e.printStackTrace();
		}
		// 请求参数封装
		MultiValueMap<String,Object> reqMap = new LinkedMultiValueMap<>();
		reqMap.add("method",method);
		reqMap.add("app_key",appKey);
		reqMap.add("partner_id","top-apitools");
		//	 reqMap.add("force_sensitive_param_fuzzy",true);
		reqMap.add("sign_method","hmac");
		reqMap.add("sign",sign);
		reqMap.add("format","json");
		reqMap.add("timestamp",timeStamp);
		reqMap.add("v",v);
		reqMap.add("activity_material_id",materialId);
		reqMap.add("adzone_id",adzone_id);
		log.info("请求接口:{}",method);
		log.info("请求参数:{}",reqMap);
		Map<String,Object> resMap = httpClientUtil.sendPost("",reqMap,url);
		log.info("返回值:{}",resMap.size());
		// 获取结果集
		Map<String,Object> responseDetail= (Map<String, Object>) resMap.get("tbk_activity_info_get_response");
		if(responseDetail == null){
			log.info("无结果集");
			return;
		}
		// 获取返回结果
		Map<String,Object> resultList = (Map<String, Object>) responseDetail.get("data");
		//ArrayList<Map<String,Object>> resultData = (ArrayList<Map<String,Object>>) resultList.get("data");
		TbkActivityInfo tbkActivityInfo = new TbkActivityInfo();
		tbkActivityInfo.setClick_url(String.valueOf(resultList.get("click_url")));
		tbkActivityInfo.setMaterial_oss_url(String.valueOf(resultList.get("material_oss_url")));
		tbkActivityInfo.setPage_end_time(String.valueOf(resultList.get("page_end_time")));
		tbkActivityInfo.setPage_name(String.valueOf(resultList.get("page_name")));
		tbkActivityInfo.setPage_start_time(String.valueOf(resultList.get("page_start_time")));
		tbkActivityInfo.setTerminal_type(String.valueOf(resultList.get("terminal_type")));
		Activity entity = activityService.getActivityByMaterialId(materialId);
		Activity upEntity = new Activity();
		// 更新活动信息
		upEntity.setId(entity.getId());
		upEntity.setActivityName(tbkActivityInfo.getPage_name());
		upEntity.setLinkUrl(tbkActivityInfo.getClick_url());
		upEntity.setType(tbkActivityInfo.getTerminal_type());
		upEntity.setGoodList(tbkActivityInfo.getMaterial_oss_url());
		upEntity.setPlatformKey(PlatformConstant.taobao.getKey());
		upEntity.setMaterialId(materialId);
		activityService.updateById2(upEntity);
		log.info("更新活动会场ID{}",materialId);
	 }

	*//**
	 * 根据物料ID拉取物料精选数据
	 * @param page
	 * @param size
	 * @param materialId
	 * @return
	 *//*
	 public List<TbkDgOptimusMaterial>  getGoosFromTBSpecialByMaterialId(Long page, Long size, String materialId){
		 // 接口方法名
		 String method = "taobao.tbk.dg.optimus.material";
		 String timeStamp = (DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
		 // 公共参数加密
		 Map<String,String> map = new HashMap<String,String>();
		 map.put("method",method);
		 map.put("app_key",appKey);
		 map.put("timestamp",timeStamp);
		 map.put("v",v);
		 map.put("sign_method","hmac");
		 map.put("format","json");
		 // 接口参数加密
		 map.put("material_id",materialId);
		 map.put("page_size",String.valueOf(size));
		 map.put("partner_id","top-apitools");
		 map.put("page_no",String.valueOf(page));
		 //	 map.put("force_sensitive_param_fuzzy","true");
		 map.put("adzone_id",String.valueOf(adzone_id));
		 String sign="";
		 // sign 加密
		 try {
			 sign = TaoBaoSecretUtil.signTopRequest(map,appSecret,"hmac");
			 log.info("加密成功：sign值为{}",sign);
		 } catch (IOException e) {
			 log.info("加密失败:{}",e.getMessage());
			 e.printStackTrace();
		 }
		 // 请求参数封装
		 MultiValueMap<String,Object> reqMap = new LinkedMultiValueMap<>();
		 reqMap.add("method",method);
		 reqMap.add("app_key",appKey);
		 reqMap.add("partner_id","top-apitools");
		 //	 reqMap.add("force_sensitive_param_fuzzy",true);
		 reqMap.add("sign_method","hmac");
		 reqMap.add("sign",sign);
		 reqMap.add("format","json");
		 reqMap.add("timestamp",timeStamp);
		 reqMap.add("v",v);
		 reqMap.add("material_id",materialId);
		 reqMap.add("page_size",size);
		 reqMap.add("page_no",page);
		 reqMap.add("adzone_id",adzone_id);
		 log.info("请求接口:{}",method);
		 log.info("请求参数:{}",reqMap);
		 Map<String,Object> resMap = httpClientUtil.sendPost("",reqMap,url);
		 log.info("返回值:{}",resMap.size());
		 // 获取结果集
		 Map<String,Object> responseDetail= (Map<String, Object>) resMap.get("tbk_dg_optimus_material_response");
		 if(responseDetail == null){
			 log.info("无结果集");
			 return new ArrayList<TbkDgOptimusMaterial>();
		 }
		 // 获取返回结果
		 Map<String,Object> resultList = (Map<String, Object>) responseDetail.get("result_list");
		 ArrayList<Map<String,Object>> resultData = (ArrayList<Map<String,Object>>) resultList.get("map_data");
		 List<TbkDgOptimusMaterial> list = new ArrayList<TbkDgOptimusMaterial>();
		 for(Map<String,Object> mapEntity: resultData){
			 Map<String,Object> valueSet= mapEntity;
			 TbkDgOptimusMaterial entity = new TbkDgOptimusMaterial();
			 // 封装返回商品实体
			 entity.setCategory_id(new Long(getDoubleParams(valueSet.get("category_id")).longValue()));
			 entity.setClick_url(String.valueOf(valueSet.get("click_url")));
			 entity.setCommission_rate(String.valueOf(valueSet.get("commission_rate")));
			 entity.setCoupon_amount(new Double(getDoubleParams(valueSet.get("coupon_amount")).intValue()));
			 entity.setCoupon_click_url(String.valueOf(valueSet.get("coupon_click_url")));
			 entity.setCoupon_end_time(String.valueOf(valueSet.get("coupon_end_time")));
			 entity.setCoupon_remain_count(getDoubleParams(valueSet.get("coupon_end_time")));
			 entity.setCoupon_share_url(String.valueOf(valueSet.get("coupon_share_url")));
			 entity.setCoupon_start_fee(String.valueOf(valueSet.get("coupon_start_fee")));
			 entity.setCoupon_start_fee(String.valueOf(valueSet.get("coupon_start_time")));
			 entity.setCoupon_total_count(getDoubleParams(valueSet.get("coupon_total_count")));
			 entity.setItem_description(String.valueOf(valueSet.get("item_description")));
			 entity.setItem_id(new Long(getDoubleParams(valueSet.get("item_id")).longValue()));
			 entity.setLevel_one_category_name(String.valueOf(valueSet.get("level_one_category_name")));
			 entity.setNick(String.valueOf(valueSet.get("nick")));
			 entity.setPict_url(String.valueOf(valueSet.get("pict_url")));
			 entity.setReserve_price(getDoubleParams(valueSet.get("reserve_price")));
			 entity.setSeller_id(getDoubleParams(valueSet.get("seller_id")));
			 entity.setShop_title(String.valueOf(valueSet.get("shop_title")));
			 entity.setSmall_images(getListParmas("small_images",valueSet));
			 entity.setSub_title(String.valueOf(valueSet.get("sub_title")));
			 entity.setTitle(String.valueOf(valueSet.get("title")));
			 entity.setUser_type(getDoubleParams(valueSet.get("user_type")));
			 entity.setVolume(new Double(getDoubleParams(valueSet.get("volume"))).intValue());
			 entity.setWhite_image(String.valueOf(valueSet.get("white_image")));
			 entity.setZk_final_price(getDoubleParams(valueSet.get("zk_final_price")));
			 list.add(entity);
		 }
		 return list;
	 }

	*//**
	 * 关键字搜索商品拉取
	 * @param page
	 * @param size
	 * @param keyWord
	 * @return
	 *//*
	 public List<TbkDgMaterialOptional> getGoodsFromTB(Long page, Long size, String keyWord){
	 	 // 接口方法名
	 	 String method = "taobao.tbk.dg.material.optional";
		 String timeStamp = (DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
		 // 公共参数加密
		 Map<String,String> map = new HashMap<String,String>();
		 map.put("method",method);
		 map.put("app_key",appKey);
		 map.put("timestamp",timeStamp);
		 map.put("v",v);
		 map.put("sign_method","hmac");
		 map.put("format","json");
		 // 接口参数加密
		 map.put("page_size",String.valueOf(size));
		 map.put("partner_id","top-apitools");
		 map.put("page_no",String.valueOf(page));
		 map.put("cat","16,18");
		 map.put("q",keyWord);
	//	 map.put("force_sensitive_param_fuzzy","true");
		 map.put("adzone_id",String.valueOf(adzone_id));
		 String sign="";
		 // sign 加密
		 try {
			 sign = TaoBaoSecretUtil.signTopRequest(map,appSecret,"hmac");
			 log.info("加密成功：sign值为{}",sign);
		 } catch (IOException e) {
		 	 log.info("加密失败:{}",e.getMessage());
			 e.printStackTrace();
		 }
		 // 请求参数封装
		 MultiValueMap<String,Object> reqMap = new LinkedMultiValueMap<>();
		 reqMap.add("method",method);
		 reqMap.add("app_key",appKey);
		 reqMap.add("partner_id","top-apitools");
	//	 reqMap.add("force_sensitive_param_fuzzy",true);
		 reqMap.add("sign_method","hmac");
		 reqMap.add("sign",sign);
		 reqMap.add("format","json");
		 reqMap.add("timestamp",timeStamp);
		 reqMap.add("v",v);
		 reqMap.add("q",keyWord);
		 reqMap.add("cat","16,18");
		 reqMap.add("page_size",size);
		 reqMap.add("page_no",page);
		 reqMap.add("adzone_id",adzone_id);
		 log.info("请求接口:{}",method);
		 log.info("请求参数:{}",reqMap);
		 Map<String,Object> resMap = httpClientUtil.sendPost("",reqMap,url);
		 log.info("返回值:{}",resMap.size());
		 // 获取结果集
		 Map<String,Object> responseDetail= (Map<String, Object>) resMap.get("tbk_dg_material_optional_response");
		 if(responseDetail == null){
		 	log.info("无结果集");
		 	return new ArrayList<TbkDgMaterialOptional>();
		 }
		 // 获取返回结果
		 Map<String,Object> resultList = (Map<String, Object>) responseDetail.get("result_list");
		 ArrayList<Map<String,Object>> resultData = (ArrayList<Map<String,Object>>) resultList.get("map_data");
		 List<TbkDgMaterialOptional> list = new ArrayList<TbkDgMaterialOptional>();
		 for(Map<String,Object> mapEntity: resultData){
		 	Map<String,Object> valueSet= mapEntity;
		 	TbkDgMaterialOptional entity = new TbkDgMaterialOptional();
		 	// 封装返回商品实体
			 entity.setCategory_id(getDoubleParams(valueSet.get("category_id")));
			 entity.setCategory_name(String.valueOf(valueSet.get("category_name")));
			 entity.setCommission_rate(String.valueOf(valueSet.get("commission_rate")));
			 entity.setCommission_type(String.valueOf(valueSet.get("commission_type")));
			 entity.setCoupon_amount(String.valueOf(valueSet.get("coupon_amount")));
			 entity.setCoupon_end_time(String.valueOf(valueSet.get("coupon_end_time")));
			 entity.setCoupon_info(String.valueOf(valueSet.get("coupon_info")));
			 entity.setCoupon_remain_count(String.valueOf(valueSet.get("coupon_remain_count")));
			 entity.setCoupon_share_url(String.valueOf(valueSet.get("coupon_share_url")));
			 entity.setCoupon_start_fee(String.valueOf(valueSet.get("coupon_start_fee")));
			 entity.setCoupon_start_time(String.valueOf(valueSet.get("coupon_start_time")));
			 entity.setCoupon_total_count(String.valueOf(valueSet.get("coupon_total_count")));
			 entity.setInclude_dxjh(String.valueOf(valueSet.get("include_dxjh")));
			 entity.setItem_description(String.valueOf(valueSet.get("item_description")));
			 entity.setItem_id(String.valueOf(valueSet.get("item_id")));
			 entity.setItem_url(String.valueOf(valueSet.get("item_url")));
			 entity.setKuadian_promotion_info(String.valueOf(valueSet.get("kuadian_promotion_info")));
			 entity.setLevel_one_category_id(String.valueOf(valueSet.get("level_one_category_id")));
			 entity.setLevel_one_category_name(String.valueOf(valueSet.get("level_one_category_name")));
			 entity.setNick(String.valueOf(valueSet.get("nick")));
			 entity.setNum_iid(String.valueOf(valueSet.get("num_iid")));
			 entity.setPict_url(String.valueOf(valueSet.get("pict_url")));
			 entity.setPresale_deposit(String.valueOf(valueSet.get("presale_deposit")));
			 entity.setPresale_end_time(String.valueOf(valueSet.get("presale_end_time")));
			 entity.setPresale_start_time(String.valueOf(valueSet.get("presale_start_time")));
			 entity.setPresale_tail_end_time(String.valueOf(valueSet.get("presale_tail_end_time")));
			 entity.setPresale_start_time(String.valueOf(valueSet.get("presale_tail_start_time")));
			 entity.setProvcity(String.valueOf(valueSet.get("provcity")));
			 entity.setReal_post_fee(String.valueOf(valueSet.get("real_post_fee")));
			 entity.setReserve_price(String.valueOf(valueSet.get("reserve_price")));
			 entity.setSale_price(String.valueOf(valueSet.get("sale_price")));
			 entity.setSeller_id(String.valueOf(valueSet.get("seller_id")));
			 entity.setShop_dsr(String.valueOf(valueSet.get("shop_dsr")));
			 entity.setShop_title(String.valueOf(valueSet.get("shop_title")));
			 entity.setSmall_images(getListParmas("small_images",valueSet));
			 entity.setSuperior_brand(String.valueOf(valueSet.get("superior_brand")));
			 entity.setTitle(String.valueOf(valueSet.get("title")));
			 entity.setTk_total_commi(String.valueOf(valueSet.get("tk_total_commi")));
			 entity.setTk_total_sales(String.valueOf(valueSet.get("tk_total_sales")));
			 entity.setUrl(String.valueOf(valueSet.get("url")));
			 entity.setUser_type(String.valueOf(valueSet.get("user_type")));
			 // Double 类型处理 转 int
			 entity.setVolume(new Double(getDoubleParams(valueSet.get("volume"))).intValue());
			 entity.setWhite_image(String.valueOf(valueSet.get("white_image")));
			 entity.setX_id(String.valueOf(valueSet.get("x_id")));
			 entity.setZk_final_price(String.valueOf(valueSet.get("zk_final_price")));
			 list.add(entity);
		 }
		 return list;
	 }

	 public  Double getDoubleParams(Object object){
	 	if(StringUtils.isEmpty(object)){
	 		return null;
		}
		 return Double.parseDouble(String.valueOf(object));
	 }

	 public  List<String> getListParmas(String filed,Map<String,Object> valueSet){
		 Map<String,Object> images  =  (Map<String,Object>)valueSet.get(filed);
		 if(images==null){
		 	return null;
		 }
		 return ((List<String>)images.get(filed));
	 }
*/

}
